package com.cmbyc.todolist.service.impl;

import com.cmbyc.todolist.service.MailService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.mail.Session;
import java.util.Date;

@Service
public class MailServiceImpl implements MailService {
    /**
     * 邮件发送器
     */
    @Resource
    private JavaMailSenderImpl mailSender;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${spring.mail.nickname}")
    private String nickname;

    @Override
    public void sendSimpleMail(String from, String[] to, Date sendDate, String subject, String content) {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(this.nickname + "<" + from + ">");
        message.setTo(to);
        if (sendDate != null) {
            message.setSentDate(sendDate);
        }
        message.setSubject(subject);
        message.setText(content);
        mailSender.send(message);
    }

    @Override
    public void sendSimpleMail(String from, String[] to, String subject, String content) {
        this.sendSimpleMail(from, to, null, subject, content);
    }

    @Override
    public void sendSimpleMail(String[] to, String subject, String content) {
        this.sendSimpleMail(this.from, to, subject, content);
    }
}
